#include "SortedArrayFind.h"

int SortedArrayFind::find(const int * data, const int row, const int column, const int val){
    #define pval(r,c)  (data + r * column + c)
    if(data == nullptr || row == 0 || column == 0) return -1;  
    int c = column - 1; 
    int r = 0;
    while(r <= (row -1) && c >= 0){
        int cur = *pval(r,c);
        if(cur == val){ 
            std::cout <<  "cur:" << cur << ",row:" << r << ",column:" << c << std::endl;
            return cur;
        }
        else if( cur < val){
            r++;
        }
        else { //cur > val
            c--;
        }
    }
    return -1;
}


#define __test__(val,rv) do { \
    int ret  = find((int*)container, row, column, (val));\
    assertEq(ret, rv); \
} while(0)
#define __test_found__(val) __test__(val,val)
#define __test_not_found__(val) __test__(val,-1);

void SortedArrayFind::run() {

    const int row = 2;
    const int column = 3;
    int container [row][column] =  {
        {1, 2, 3},
        {3, 5, 7},
    };

    util::TwoDimensionalArray::print((int*)container, row, column);

    __test_not_found__(10);
    __test_found__(2);
    __test_found__(1);
    __test_found__(3);
    __test_found__(5);
    __test_found__(7);
}
